// 基于准备好的dom，初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));//定义折线图显示的位置对象
var option = {
    title: {
        text: '学院到课率横向比'
    },
    legend: {
        data:[]
    },
    xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
        type: 'value',
        axisLabel: {
            formatter: '{value} %'
        }
    },
    series: [ {
        name:'邮件营销',
        type:'line',
        stack: '到课率',
        data:[120, 132, 101, 134, 90, 230, 210]
    },
        {
            name:'联盟广告',
            type:'line',
            stack: '到课率',
            data:[220, 182, 191, 234, 290, 330, 310]
        },]
};
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "menuId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    },
    check:{
        enable:true,
        nocheckInherit:true
    }
};
var ztree;

$(function () {
    //加载菜单树
    $.get(baseURL + "/AttendenceCharts/getProfessionTree", function(r){
        ztree = $.fn.zTree.init($("#zTree"), setting, r.r);
        //展开所有节点
        ztree.expandAll(false);
    });
})
function query()
{
    //获取选择的菜单
    var nodes = ztree.getCheckedNodes(true);
    var profession="";
    for(var i=0; i<nodes.length; i++) {
        profession+=nodes[i].menuId+',';
    }
    $.ajax({
        url: baseURL + "AttendenceCharts/getAttendanceProfessionHorizontalComparison",
        data: {
            "profession":profession,
            "startDate":$("#startDate").val(),
            "endDate":$("#endDate").val()
        },
        success: function (r) {
            option.series=r.r.data;
            option.xAxis= {
                type: 'category',
                data:r.r.xAxis
            };
            option.legend.data=r.r.legend;
            myChart.setOption(option);
        }
    });
}
function setDate(id) {
    var Nowdate=new Date();
    M=Number(Nowdate.getMonth())+1;
    var today=Nowdate.getFullYear()+"-"+fix(M)+"-"+fix(Nowdate.getDate());
    if(id=='1'){
        $("#startDate").val(today);
        $("#endDate").val(today);
    }else if(id=='2'){
        $("#startDate").val(showWeekFirstDay());
        $("#endDate").val(today);
    }if(id=='3'){
        $("#startDate").val(showMonthFirstDay());
        $("#endDate").val(today);
    }
    query()
}
function showWeekFirstDay()
{
    var Nowdate=new Date();
    var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
    M=Number(WeekFirstDay.getMonth())+1
    return WeekFirstDay.getFullYear()+"-"+fix(M)+"-"+fix(WeekFirstDay.getDate());
}
function showWeekLastDay()
{
    var Nowdate=new Date();
    var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
    var WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);
    M=Number(WeekLastDay.getMonth())+1
    return WeekLastDay.getFullYear()+"-"+M+"-"+WeekLastDay.getDate();
}
function showMonthFirstDay()
{
    var Nowdate=new Date();
    var MonthFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth(),1);
    M=Number(MonthFirstDay.getMonth())+1
    return MonthFirstDay.getFullYear()+"-"+fix(M)+"-"+fix(MonthFirstDay.getDate());
}
function fix(num){
    if(num<10)
        return "0"+num;
    else return ""+num;
}
function showMonthLastDay()
{
    var Nowdate=new Date();
    var MonthNextFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth()+1,1);
    var MonthLastDay=new Date(MonthNextFirstDay-86400000);
    M=Number(MonthLastDay.getMonth())+1
    return MonthLastDay.getFullYear()+"-"+M+"-"+MonthLastDay.getDate();
}